﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.ServiceHosting.StorageClient;
using Microsoft.ServiceHosting.ServiceRuntime;
using System.Collections.Specialized;
using System.IO;
using UlyssesAgenda.Storage.Table;

namespace UlyssesAgenda.Repositories.Blog
{
    public class BlogEntryRepository : CloudStorageRepositoryBase
    {
        public BlogEntryRepository(StorageAccountInfo blob, StorageAccountInfo queue, StorageAccountInfo table)
            : base(blob, queue, table)
        {
            InitBlobContainer(RoleManager.GetConfigurationSetting("BlogEntryContainer"));
            InitTableStorage(RoleManager.GetConfigurationSetting("BlogEntryTableName"));
        }

        public BlogEntryInfo[] GetBlogEntries(int pageSize, int page)
        {
            List<BlogEntry> blogTableEntries = new List<BlogEntry>(GameTableStorage.GetBlogEntries(tableStorage, pageSize, page));
            List<BlogEntryInfo> blogEntries = new List<BlogEntryInfo>();

            foreach (BlogEntry be in blogTableEntries)
            {
                BlobContents contents = new BlobContents(new MemoryStream());
                System.Text.UnicodeEncoding unicode16 = new UnicodeEncoding();
                BlobProperties props = blobContainer.GetBlob(be.CanonicalName, contents, false);
                blogEntries.Add(new BlogEntryInfo()
                 {
                     Url = props.Uri.ToString(),
                     PostHtml = unicode16.GetString(contents.AsBytes()),
                     CreateDateTime = be.CreateDateTime,
                     Title = be.Title,
                     Author = be.Author,
                     CanonicalName = be.CanonicalName
                 });
            }
            return blogEntries.ToArray();
        }

        //public BlogEntryInfo GetBlogEntry(string canonicalName)
        //{
        //    BlobContents contents = new BlobContents(new MemoryStream());
        //    BlobProperties props = blobContainer.GetBlob(canonicalName, contents, false);
        //    NameValueCollection metadata = props.Metadata;
        //    BlogEntryInfo info = new BlogEntryInfo()
        //    {
        //        Url = props.Uri.ToString(),
        //        CanonicalName = canonicalName,
        //        CreateDateTime = DateTime.Parse(metadata["CreateDateTime"]),
        //        Title = metadata["Title"],
        //        Author = metadata["Author"]
        //    };
        //    return info;
        //}

        public void CreateBlogEntry(string title, string author, string postHtml)
        {
            BlobProperties properties = new BlobProperties(title);

            System.Text.UnicodeEncoding unicode16 = new UnicodeEncoding();
            byte[] postBytes = unicode16.GetBytes(postHtml.ToCharArray());

            // Create the blob
            BlobContents fileBlob = new BlobContents(postBytes);
            blobContainer.CreateBlob(properties, fileBlob, true);

            // Create the table row
            GameTableStorage.InsertBlogEntry(tableStorage, title, author, title);
        }
    }
}
